/*
 * Copyright (C) 2020 UNISOC Open Source Project
 *
 */

package com.unisoc.systemui.screenshot;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import com.android.systemui.screenshot.GlobalScreenshotMoreAction;

/**
 * An AsyncTask that compare two images in the background.
 * This do not need context :)
 */
public class CompareTask extends AsyncTask<Void, Void, Boolean> {
    private static final String TAG = "CompareTask";
    private final GlobalScreenshotMoreAction.CompareImageInBackgroundData mParams;

    private final int mScreenWidth;
    private final int mScreenHeight;
    //UNISOC: Add for bug 1361055
    private final float mScale;
    private final int mHeader;
    private final int mBottom;
    private int mDistance;

    public CompareTask(GlobalScreenshotMoreAction.CompareImageInBackgroundData data) {
        mParams = data;
        mScreenWidth = mParams.currentBitmap.getWidth();
        mScreenHeight = mParams.currentBitmap.getHeight();
        /*UNSOC: Modify for bug 1361055 @{*/
        mScale = mParams.scale;
        mHeader = (int) (mScreenHeight / 4 * mScale);
        mBottom = (int)(mScreenHeight / 4 * mScale);
        /*}@*/
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        Log.d(TAG, "begin compare task.");
        if (mParams.currentBitmap == null) {
            Log.w(TAG, "bitmap is null");
            return false;
        }

        if (mParams.previousBitmap == null) {
            // Croped first bitmap
            return true;
        }
        /*UNISOC: Add for bug 1361055 @{*/
        if (CompareUtils.CompareCropedBitmap(mParams.previousBitmap, mParams.currentBitmap, mScale)) {
            Log.w(TAG, "same as previous bitmap");
            return false;
        }

        mDistance = CompareUtils.computeMoveDistance(mParams.previousBitmap, mParams.currentBitmap, mScale);
        /*}@*/
        if (mDistance == -1) {
            Log.w(TAG, "distance is -1");
            return false;
        }

        if (mScreenHeight - mHeader - mBottom - mDistance <= CompareUtils.COMPARE_IGNORE_DISTANCE) {
            Log.w(TAG, "same height");
            return false;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            int x = 0;
            int y = mDistance + mHeader;
            int w = mParams.currentBitmap.getWidth();
            int h = mParams.currentBitmap.getHeight() - y - mBottom;

            Bitmap croped = null;
            if (mParams.previousBitmap == null) {
                // Croped first bitmap
                croped = Bitmap.createBitmap(mParams.currentBitmap, 0, 0, mScreenWidth, mScreenHeight - mBottom);
            } else {
                croped = Bitmap.createBitmap(mParams.currentBitmap, x, y, w, h);
            }
            mParams.compareCompleteListener.onCompareComplete(croped);
        } else {
            // Unable to find any mapped bitmaps
            mParams.compareCompleteListener.onCompareComplete(null);
        }
        Log.d(TAG, "end compare task.");
    }

    @Override
    protected void onCancelled(Boolean result) {
        mParams.compareCompleteListener.onCompareComplete(null);
        mParams.clearImage();
    }
}
